package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.mapper.DeviceMapper;
import com.terabits.smartriver.mapper.ModelMapper;
import com.terabits.smartriver.mapper.ModelSettingMapper;
import com.terabits.smartriver.meta.po.*;
import com.terabits.smartriver.meta.vo.ModelVO;
import com.terabits.smartriver.service.ModelService;
import com.terabits.smartriver.util.CalculationUtils;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.swing.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;

@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ModelSettingMapper modelSettingMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List getModel(Integer modelId) {
        return modelMapper.selectModel(modelId);
    }

    @Override
    public List<PageData> getModelByProject(PageData params) {
//        //通过项目编号获取模型Id集
//        List<Integer> listModelId = modelMapper.selectListModelIdByProjectNum(params);
//        if (listModelId.size()==0){
//            return null;
//        }
        return modelMapper.selectModelById(params);
    }

    @Override
    public int addModel(Model model) {
        String checkOperator = CalculationUtils.checkOperator(model.getExpression(), model.getType());
        if (checkOperator == null) {
            return -1;
        }

        //将参数随机赋值查看是否合法
        String expressionTest = CalculationUtils.replace(model.getExpression(), model.getType());

        String calculation = CalculationUtils.calculation(expressionTest);

        //如果返回值是0说明转换有误 格式错误
        if (calculation == null) {
            return -1;
        }
        model.setModelId((int) new Double((Math.random() + 1) * Math.pow(10, 10 - 1)).longValue());
        return modelMapper.insertSelective(model);
    }


    @Override
    public int updateModel(Model model) {
        Model modelInfo = new Model();
        modelInfo.setSpecification(model.getSpecification());
        modelInfo.setDescription(model.getDescription());
        Example example = new Example(Model.class);
        example.createCriteria().andEqualTo("modelId", model.getId());
        return modelMapper.updateByExampleSelective(modelInfo, example);
    }

    @Override
    public int addModelSetting(List<ModelSetting> modelSettings) {
        int res = 0;
        for (ModelSetting setting : modelSettings) {
            if (StringUtils.isEmpty(setting.getMin())) {
                setting.setMin(Integer.MIN_VALUE + "");
            }
            if (StringUtils.isEmpty(setting.getMax())) {
                setting.setMax(Integer.MAX_VALUE + "");
            }
            res += modelSettingMapper.insertSelective(setting);

        }
        return res;
    }

    @Override
    public int updateModelSetting(List<ModelSetting> modelSettings) {
        int res = 0;
        for (ModelSetting setting : modelSettings) {
            res += modelSettingMapper.updateByPrimaryKeySelective(setting);
        }
        return res;
    }

    @Override
    public int deleteModelSetting(List<Integer> ids) {
        int res = 0;
        for (Integer id : ids) {
            res += modelSettingMapper.deleteByPrimaryKey(id);
        }
        return res;
    }

    @Override
    public int addModelDevice(PageData pageData) {
        String modelId = (String) pageData.get("modelId");
        String devices = (String) pageData.get("devices");
        if (!StringUtils.isEmpty(modelId) && !StringUtils.isEmpty(devices)) {
            //如果模型设备绑定的id为空，说明他 们不存在绑定关系，做添加操作
            List<PageData> pageData1 = modelMapper.selectModelDeviceByDeviceId(pageData);
            if (pageData1.size() == 0) {
                Device device = new Device();
                device.setDeviceNum(devices);
                Device deviceInfo = deviceMapper.selectOne(device);
                deviceInfo.setIsnotbinding(1);
                deviceMapper.updateByPrimaryKeySelective(deviceInfo);
                return modelMapper.insertModelDevice(pageData);
            }
            return modelMapper.updateModelDevice(pageData);
        }
        return 0;
//        //反之做修改操作
    }

    @Override
    public int updateModelDevice(PageData pageData) {
        return modelMapper.updateModelDevice(pageData);
    }

    @Override
    public int delModelDevice(Integer id) {
        Model model = new Model();
        model.setModelId(id);
        modelMapper.delete(model);
        int i = modelMapper.deleteModelDevice(id);
        if (i > 0) {
            PageData pageData = new PageData();
            pageData.put("modelId", id);
            List<PageData> info = modelMapper.selectModelDevice(pageData);
            if (info.size() > 0) {
                List<String> devices = info.stream().map(pageDatas -> pageDatas.getString("devices")).collect(Collectors.toList());
                if (devices.size() > 0) {
                    for (String device : devices) {
                        Device device1 = new Device();
                        device1.setIsnotbinding(0);
                        Example example = new Example(Device.class);
                        example.createCriteria().andEqualTo("deviceNum", device);
                        deviceMapper.updateByExampleSelective(device1, example);
                    }
                }
            }
        }
        ModelSetting modelSetting = new ModelSetting();
        modelSetting.setModelId(id);
        return modelSettingMapper.delete(modelSetting);
    }

    @Override
    public List getModelDevice(PageData pageData) {
        return modelMapper.selectModelDevice(pageData);
    }

}